package com.example.udpscreen.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.util.EncodingUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.udpscreen.models.ResumeFileRecord;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

public class FileUtil {

	
	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		return sdDir.toString();

	}
	
	public static String ApplicationPath=getSDPath()+"/ad";
	public static String ResumeFilePath=ApplicationPath+"/resumefile";
	
	public static String MonitorFileTemp=ApplicationPath+"/monitor.png";
	
	public static String ResumeFileRecordsFilePath =ResumeFilePath +"/fileRecord.json";
	
	public static String RecvFilePath=ApplicationPath+"/files";
	
	
	public static void initFilePath(){
		
		File file = new File(ApplicationPath);
		if(!file.exists())
			file.mkdir();
		file = new File(ResumeFilePath);
		if(!file.exists())
			file.mkdir();
		file = new File(RecvFilePath);
		if(!file.exists())
			file.mkdir();
	}
	
	// UTF-8
		public static String readFile(String file_path) {
			String res = "";
			try {
				FileInputStream fin = new FileInputStream(file_path);
				// FileInputStream fin = openFileInput(fileName);
				// 用这个就不行了，必须用FileInputStream
				int length = fin.available();
				byte[] buffer = new byte[length];
				fin.read(buffer);
				res = EncodingUtils.getString(buffer, "UTF-8");
				fin.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (res != null && res.startsWith("\ufeff")) {
				res = res.substring(1);
			}
			return res;

		}
		
		
		/**
		 * 
		 * @param file_path
		 * @param content
		 * @return
		 */
		public static boolean writeFile(String file_path, String content) {
			boolean res = false;
			try {
				FileOutputStream fout = new FileOutputStream(file_path);
				fout.write(content.getBytes(), 0, content.getBytes().length);
				fout.flush();
				fout.close();
				res = true;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return res;
		}


		/**
		 * 以最省内存的方式读取本地资源的图片
		 * 
		 * @param context
		 * @param resId
		 * @return
		 */
		public static Bitmap readBitMap(Context context, int resId) {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true;
			// 获取资源图片
			InputStream is = context.getResources().openRawResource(resId);

			return BitmapFactory.decodeStream(is, null, opt);
		}

		public static Bitmap readBitMap(String path) {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true;
			
			return BitmapFactory.decodeFile(path, opt);
		}
	
}
